package cn.com.zhuxulin.info.Utils;

import java.io.IOException;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Provices extends DefaultHandler {
	private String[] provices = new String[34];
	private String proviceURL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince";

	public Provices() {
	}

	public String[] getProvices() throws ParserConfigurationException, SAXException, FactoryConfigurationError, IOException {
		String tmp = HttpRequest.doGet(proviceURL);
		// System.out.println(tmp);
		// DefaultHandler provice=new ParseProvices();
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		parser.parse(HttpRequest.doGetForInputStream(proviceURL), this);
		return provices;
	}

	// private class ParseProvices extends DefaultHandler{
	private boolean flag = false;
	private int point = -1;

	public void characters(char[] ch, int start, int length) throws SAXException {
		if (flag) {
			// String tmp=new String(ch, 0, length);
			// System.out.println(point);
			provices[point] = new String(ch, 0, length);
			// System.out.println(tmp);
			// System.out.println(point+":"+provices[point]);
			flag = false;
		}
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (qName.equals("string")) {
			flag = true;
			point++;
		}
		// else {
		// flag = false;
		// }
//		System.out.println("URI:" + uri + "\nlocalName:" + localName + "\nqName:" + qName + "\nattrValue:" + attributes.getValue(0));
	}
}
